package com.samoiloff.util.actions {
	import com.samoiloff.util.actions.Action;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	/**
	 * @author Samoiloff  http://samoiloff.com/
	 */
	public class LoadMovieActionItem extends Action {
		
		protected var _urlRequest	: URLRequest;
		protected var _loader		: Loader;
		
		public function LoadMovieActionItem() {
			super();
		}
		
		override public function start() : void {
			super.start();

			_loader = new Loader();
			_loader.load(_urlRequest, new LoaderContext(false));
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onOIError);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			
		}

		protected function onProgress(event : ProgressEvent) : void {
			throw new Error("this function should be overriden");
		}

		protected function onOIError(event : IOErrorEvent) : void {
			throw new Error("this function should be overriden");
		}

		protected function onComplete(event : Event) : void {
			throw new Error("this function should be overriden");
		}
		
		
		override public function destroy() : void {
			_urlRequest = null;

			if (_loader) {
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
				_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onOIError);
				_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			}
			
			super.destroy();
		}
		
	}
}
